<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/7/30
 * Time: 17:22
 */

namespace app\commi\controller;

use think\Db;
use think\Request;

class Rowingyy extends Base
{
    /**
     * 赛艇临时成绩异议列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        $match = Db::table('up_match')
            ->where('match_committee_id', session('com_id'))
            ->field('match_id')
            ->find();
        
        if(request()->isAjax ()){
//            $keyword = trim(input('keyword'), ' '); // 船长姓名/ID
            $ls_status = input('ls_status'); // 状态
            $where['umtr.match_id'] = $match['match_id'];
            
//            if(isset($keyword) && $keyword != ''){
//                $where['user_real_name|user_create_id'] = ['like', '%' . $keyword . '%'];
//            }
            if(isset($ls_status) && $ls_status != ''){
                $where['umyr.ls_status'] = $ls_status;
            }else{
                $where['umyr.ls_status'] = ['NEQ', 1];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up__match_yacht_record')
                ->alias('umyr')
                ->join('up__match_turns_rounds umtr', 'umtr.id = umyr.match_turns_rounds_id', 'left')
                ->join('up_u_user uuu', 'uuu.user_id = umyr.user_id', 'left')
                ->join('up_match_group umg', 'umg.mg_id = umtr.ma_group', 'left')
                ->join('up__match_rounds umr', 'umr.mr_id = umtr.mr_id', 'left')
                ->join('up__match_turns umt', 'umt.mt_id = umtr.mt_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up__match_yacht_record')
                ->alias('umyr')
                ->join('up__match_turns_rounds umtr', 'umtr.id = umyr.match_turns_rounds_id', 'left')
                ->join('up_u_user uuu', 'uuu.user_id = umyr.user_id', 'left')
                ->join('up_match_group umg', 'umg.mg_id = umtr.ma_group', 'left')
                ->join('up__match_rounds umr', 'umr.mr_id = umtr.mr_id', 'left')
                ->join('up__match_turns umt', 'umt.mt_id = umtr.mt_id', 'left')
                ->field('umyr.id, umg.mg_name, umtr.ma_distance, umtr.ma_boat_type, umt.mt_name, 
                umr.mr_name, umyr.track_num, umyr.ls_status, umyr.user_id, umtr.match_id, umyr.temp_score, 
                umyr.ls_change_score, umyr.ls_yiyi_content, umyr.ls_commit_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('umyr.temp_score asc')
                ->select();
            foreach($lists as $k => $v){
                // 名次
                $lists[$k]['mc'] = $k + 1;
                // 报名信息
                $apply = Db::table('up_match_apply_info')
                    ->where('ma_user_id', $v['user_id'])
                    ->where('ma_match_id', $v['match_id'])
                    ->field('ma_team_name')
                    ->find();
                $lists[$k]['ma_team_name'] = $apply['ma_team_name']; // 队名
                $lists[$k]['member_type'] = session('cm_member_type');
        
                $join_user = Db::table('up_match_apply_info')
                    ->where('ma_team_name', $apply['ma_team_name'])
                    ->where('ma_match_id', $v['match_id'])
                    ->column('ma_user_name');
                $lists[$k]['ma_user_name'] = implode(',', $join_user);
            }
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        
        return $this->fetch();
    }
    
    /**
     * 修改临时成绩
     * @return mixed|\think\response\Json
     */
    public function changeMark(){
        $id = input('id');
        if(Request::instance()->isPost()){
            $ls_change_score = input('ls_change_score');
            $update = [
                'ls_change_score' => $ls_change_score,
                'ls_status' => 3,
            ];
            $res = Db::table('up__match_yacht_record')->where('id', $id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '修改成功']);
            }else{
                return json(['code' => 100, 'msg' => '修改失败']);
            }
        }
        $this->assign([
            'id' => $id
        ]);
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            return $this->fetch('rowingyy/changemark');
        }
    }
    
    /**
     * 确认修改成绩
     * @return \think\response\Json
     */
    public function commitMark(){
        $lose = $this->lostTime();
        if($lose == 1){
            return json(['code' => 100, 'msg' => '账号已过期，非法操作']);
        }
        $id = input('id');
        $ls = Db::table('up__match_yacht_record')
            ->where('id', $id)
            ->field('ls_change_score')
            ->find();
        $update = [
            'temp_score' => $ls['ls_change_score'],
            'ls_status' => 5
        ];
        $res = Db::table('up__match_yacht_record')->where('id', $id)->update($update);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 驳回成绩
     * @return \think\response\Json
     */
    public function refsualMark(){
        $lose = $this->lostTime();
        if($lose == 1){
            return json(['code' => 100, 'msg' => '账号已过期，非法操作']);
        }
        $id = input('id');
        $update = [
            'ls_change_score' => null,
            'ls_yiyi_content' => null,
            'ls_status' => 4
        ];
        $res = Db::table('up__match_yacht_record')->where('id', $id)->update($update);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}